﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class PostRole : System.Web.UI.Page
    {
        private int editid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterRole"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (!IsPostBack)
            {

                if (editid > 0)
                {
                    RoleInfo roleInfo = Role.GetRoleInfo(editid);
                    Name.Text = roleInfo.Name;
                    LoadMenu(roleInfo.RoleMain);
                }
                else
                {
                    LoadMenu("");
                }
            }
        }

        protected void LoadMenu(string RoleMain)
        {
            MenuList.Items.Clear();
            IList<MenuInfo> menuInfos = Menus.GetMenuInfos(0, 0);
            foreach (MenuInfo info in menuInfos)
            {
                ListItem item = new ListItem(info.Name, info.Page);
                if (("," + RoleMain + ",").IndexOf(info.Page) > -1)
                {
                    item.Selected = true;
                }
                MenuList.Items.Add(item);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Name.Text.Trim() == "")
            {
                Utility.ShowJsAlert("请填写组标题", this);
                return;
            }
            else if (MenuList.SelectedIndex == -1)
            {
                Utility.ShowJsAlert("必须至少选择一项权限", this);
                return;
            }
            else
            {
                List<string> items = new List<string>();
                foreach (ListItem item in MenuList.Items)
                {
                    if (item.Selected == true)
                    {
                        items.Add(item.Value);
                    }
                }
                RoleInfo roleInfo = new RoleInfo();
                roleInfo.Name = Name.Text;
                roleInfo.RoleMain = string.Join(",", items.ToArray());
                if (editid > 0)
                {
                    roleInfo.Id = editid;
                    Role.EditRole(roleInfo);
                    Utility.ReloadTopPage(this);
                }
                else
                {
                    Role.AddRole(roleInfo);
                    Utility.ReloadTopPage(this);
                }
            }
        }
    }
}
